package com.w3c.pragmatic.controller;

import com.w3c.pragmatic.utils.FileUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.net.URL;

/**
 * 文件操作 前端控制器
 *
 * @author wangdong
 * @since 2025-02-20
 */
@Slf4j
@RestController
@RequestMapping("/file")
@Api(tags = "文件操作控制层FileOperatorController")
public class FileOperatorController {

  @Resource private FileUtil fileUtil;

  /** 下载（按路径下载） */
  @ApiOperation(value = "下载（按路径下载）", httpMethod = "POST")
  @RequestMapping(
      value = "/downloadByPath/{fileName}",
      method = RequestMethod.POST,
      produces = MediaType.APPLICATION_JSON_VALUE)
  public void downloadByPath(@PathVariable String fileName, HttpServletResponse response) {
    /*String localFilePath = "C:\\Users\\pragm\\Desktop\\temp-doc\\XY0082.pdf";
    fileUtil.downloadLocalFile(localFilePath, fileName, response);*/

    String urlFilePath =
        "https://channel-oss-cdn.xiaoxiangyoupin.cn/test/flowcloud/signature/QSLS347460488824913920682_XY0081_个人信息使用和第三方机构数据查询授权书.pdf";
    fileUtil.downloadUrlFile(urlFilePath, fileName, response);
  }

  /** 下载（按输入流下载） */
  @ApiOperation(value = "下载（按输入流下载）", httpMethod = "POST")
  @RequestMapping(
      value = "/downloadByInputStream/{fileName}",
      method = RequestMethod.POST,
      produces = MediaType.APPLICATION_JSON_VALUE)
  public void downloadByInputStream(@PathVariable String fileName, HttpServletResponse response)
      throws Exception {
    String filePath =
        "https://channel-oss-cdn.xiaoxiangyoupin.cn/test/flowcloud/signature/QSLS347460488824913920682_XY0081_个人信息使用和第三方机构数据查询授权书.pdf";
    URL url = new URL(filePath);
    fileUtil.downloadFile(url.openConnection().getInputStream(), fileName, response);
  }

  /** 上传 */
  @ApiOperation(value = "上传", httpMethod = "POST")
  @RequestMapping(
      value = "/upload",
      method = RequestMethod.POST,
      produces = MediaType.APPLICATION_JSON_VALUE)
  public void uploadFile(MultipartFile file) {
    String storagePath = "C:\\upload\\file\\";
    fileUtil.uploadFile(file, "协议模板.pdf", storagePath);
  }

  /** 上传 */
  @ApiOperation(value = "转成File对象", httpMethod = "POST")
  @RequestMapping(
      value = "/transfer",
      method = RequestMethod.POST,
      produces = MediaType.APPLICATION_JSON_VALUE)
  public void transferToFile(MultipartFile multipartFile) {
    File file = fileUtil.multipartToFile(multipartFile);
    log.info("file1====>{}", file.getPath());
    log.info("file2====>{}", file.getName());
  }

  /** 读和写文件内容 */
  @ApiOperation(value = "读和写文件内容(字符)", httpMethod = "POST")
  @RequestMapping(
      value = "/readAndWriteStr",
      method = RequestMethod.POST,
      produces = MediaType.APPLICATION_JSON_VALUE)
  public void readAndWriteStr() {
    // 读取文件
    String sourcePath = "C:\\Users\\pragm\\Desktop\\temp-doc\\工作日程.txt";
    String content = fileUtil.readFileToString(sourcePath);
    // 写入文件
    String targetPath = "C:\\upload\\XY000000082.txt";
    fileUtil.writeStringToFile(targetPath, content);
  }

  /** 读和写文件内容 */
  @ApiOperation(value = "读和写文件内容(字节)", httpMethod = "POST")
  @RequestMapping(
      value = "/readAndWriteByte",
      method = RequestMethod.POST,
      produces = MediaType.APPLICATION_JSON_VALUE)
  public void readAndWriteByte() {
    // 读取文件
    String sourcePath = "C:\\Users\\pragm\\Desktop\\temp-doc\\XY0082.pdf";
    byte[] content = fileUtil.readFileToByteArray(sourcePath);
    // 写入文件
    String targetPath = "C:\\upload\\XY000000082.pdf";
    fileUtil.writeByteArrayToFile(targetPath, content);
  }

  /** 读取文件内容 */
  @ApiOperation(value = "复制文件", httpMethod = "POST")
  @RequestMapping(
      value = "/copyFile",
      method = RequestMethod.POST,
      produces = MediaType.APPLICATION_JSON_VALUE)
  public void copyFile() {
    // 读取文件
    String sourceFilePath = "C:\\Users\\pragm\\Desktop\\temp-doc\\XY0082.pdf";
    String destFilePath = "C:\\upload\\XY0083.pdf";
    fileUtil.copyFile(sourceFilePath, destFilePath);
  }
}
